#include "middle.h"
#define LOG_TAG          "MID_MODULE" 
#include "elog.h"

mmc_ctrl_t midlayer_sample_tempraure_mmc_ctrl;
mmc_ctrl_t midlayer_sample_bright_mmc_ctrl;
mmc_ctrl_t midlayer_switch_light_mmc_ctrl;
mmc_ctrl_t midlayer_switch_fan_mmc_ctrl;

midlayer_sample_temperature_t midlayer_sample_temperature_values;
midlayer_sample_bright_t     midlayer_sample_bright_values;
midlayer_switch_light_t      midlayer_switch_light_values;
midlayer_switch_fan_t        midlayer_switch_fan_values;


TaskHandle_t            mid_sample_moduel_task_handler;
TaskHandle_t            mid_control_moduel_task_handler;
// 任务函数
void mid_sample_task_process_entry(void* p);
void mid_control_task_process_entry(void* p);




/**
 * @desc  : 中间层初始化
 * @date  : 2025-11-06
 * @author: AnRui
 * @note  : 中间层: 采集模块 控制模块 freemodbus
 */
void middle_layer_init()
{
    log_d("middle_layer_init start\r\n");
    // 中间层和适配层数据初始化
    mmc_ctrl_init(&midlayer_sample_tempraure_mmc_ctrl,
        &midlayer_sample_temperature_values,
        sizeof(midlayer_sample_temperature_t));
    mmc_ctrl_init(&midlayer_sample_bright_mmc_ctrl,
        &midlayer_sample_bright_values,
        sizeof(midlayer_sample_bright_t));
    mmc_ctrl_init(&midlayer_switch_light_mmc_ctrl,
        &midlayer_switch_light_values,
        sizeof(midlayer_switch_light_t));
    mmc_ctrl_init(&midlayer_switch_fan_mmc_ctrl,
        &midlayer_switch_fan_values,
        sizeof(midlayer_switch_fan_t));
	
    // 采集和控制模块任务创建
    xTaskCreate((TaskFunction_t)mid_sample_task_process_entry,
        "midlayer_sample_process_task",
        MID_SAMPLE_MODUEL_TASK_STACK_SIZE,
        NULL,
        MID_SAMPLE_MODUEL_TASK_PRIORITY,
        (TaskHandle_t*)&mid_sample_moduel_task_handler);
    xTaskCreate((TaskFunction_t)mid_control_task_process_entry,
        "midlayer_control_process_task",
        MID_CONTROL_MODUEL_TASK_STACK_SIZE,
        NULL, MID_CONTROL_MODUEL_TASK_PRIORITY,
        (TaskHandle_t*)&mid_control_moduel_task_handler);
    // 适配层初始化
    log_d("middle_layer_init end\r\n");
}


/**
 * @desc  : 采集模块任务
 * @date  : 2025-11-06
 * @author: AnRui
 * @param : p - {参数说明}:
 * @note  :
 */
void mid_sample_task_process_entry(void* p)
{
    log_d("mid sample task start");
    // 临时指针：简化共享内存转换
    busi_sample_share_value_t* p_busi_sample = (busi_sample_share_value_t*)busi_sample_mmc.p_shared_mem;
    while (1) {
        //等待来自业务的采集请求
        xSemaphoreTake(busi_sample_mmc.sem_req, portMAX_DELAY);
        // 向适配层 发送信号开始温度采集 适配层任务开始采集 并把数据放到中间层数据区域 中间层在将数据填写到busi层数据区域
        if (mmc_ctrl_require(&midlayer_sample_tempraure_mmc_ctrl, MID_WAIT_TEMP_SAMPLE_TIME) == true) {
            p_busi_sample->temp = midlayer_sample_temperature_values.temperature;
            p_busi_sample->temp_valid = midlayer_sample_temperature_values.is_valid;
        } else {
            midlayer_sample_temperature_values.is_valid = false;
            p_busi_sample->temp_valid = false;
            log_e("midlayer sample temperature fail");
        }

        // 向适配层 发送信号开始亮度采集 适配层任务开始采集 并把数据放到中间层数据区域 中间层在将数据填写到busi层数据区域
        if (mmc_ctrl_require(&midlayer_sample_bright_mmc_ctrl, MID_WAIT_BRIGHT_SAMPLE_TIME) == true) {
            p_busi_sample->brightness = midlayer_sample_bright_values.bright;
            p_busi_sample->bright_valid = midlayer_sample_bright_values.is_valid;
        } else {
            midlayer_sample_bright_values.is_valid = false;
            p_busi_sample->bright_valid = false;
            log_e("midlayer sample bright fail");
        }
        p_busi_sample->reserve[0] = 1;
        p_busi_sample->reserve[1] = 3;
        p_busi_sample->reserve[2] = 4;
        p_busi_sample->reserve[3] = 5;
        // 回复来自业务的采集请求
        xSemaphoreGive(busi_sample_mmc.sem_ack);
    }
}


/**
 * @desc  : 控制模块任务
 * @date  : 2025-11-06
 * @author: AnRui
 * @param : p - {参数说明}:
 * @note  : 控制：1.传递业务层指令 2.请求适配层执行 3.反馈结果
 */
void mid_control_task_process_entry(void* p)
{
    log_d("mid control task start");
    // 临时指针：简化共享内存转换
    busi_control_share_value_t* p_busi_control = (busi_control_share_value_t*)busi_control_mmc.p_shared_mem;
    while (1) {
        //等待来自业务的控制请求
        xSemaphoreTake(busi_control_mmc.sem_req, portMAX_DELAY);
        //传递控制指令
        midlayer_switch_light_values.light_status = p_busi_control->light_status;
        midlayer_switch_fan_values.fan_status = p_busi_control->fan_status;
				log_d("midlayer resv cmd, fan_status : %s , light_status : %s",
                p_busi_control->fan_status == FAN_STATUS_ON ? "风扇打开" : "风扇关闭",
                p_busi_control->light_status == LIGHT_STATUS_ON ? "电灯打开" : "电灯关闭");
        // 向适配层 发送信号开始控制灯光
        if (mmc_ctrl_require(&midlayer_switch_light_mmc_ctrl, MID_WAIT_LIGHT_CONTROL_TIME) == true) {
            p_busi_control->light_status = midlayer_switch_light_values.light_status;
        } else {
            midlayer_switch_light_values.light_status = LIGHT_STATUS_FAULT;
            p_busi_control->light_status = LIGHT_STATUS_FAULT;
            log_e("midlayer to switch light adapter layer communicate timeout");
        }
        // 向适配层 发送信号开始控制风扇
        if (mmc_ctrl_require(&midlayer_switch_fan_mmc_ctrl, MID_WAIT_FAN_CONTROL_TIME) == true) {
            p_busi_control->fan_status = midlayer_switch_fan_values.fan_status;
        } else {
            midlayer_switch_fan_values.fan_status = FAN_STATUS_FAULT;
            p_busi_control->fan_status = FAN_STATUS_FAULT;
            log_e("midlayer to switch fan adapter layer communicate timeout");
        }
        p_busi_control->reserve[0] = 1;
        p_busi_control->reserve[1] = 3;
        p_busi_control->reserve[2] = 4;
        p_busi_control->reserve[3] = 5;
        // 回复来自业务的控制请求
        xSemaphoreGive(busi_control_mmc.sem_ack);
    }
}

